<?php
/**
 * Created by PhpStorm.
 * User: 666
 * Date: 2020/4/6
 * Time: 16:39
 */

namespace App\HttpController\Admin;


use App\Consts\RedisKey;
use App\HttpController\BaseController;
use App\Model\Admin;
use App\Utility\Register;
use App\Validate\ValidateTool;
use EasySwoole\Http\Message\Status;
use App\Utility\Response;
use EasySwoole\RedisPool\Redis;

class AdminBaseController extends  BaseController
{

    public function index()
    {
    }


//    function onException(\Throwable $throwable): void
//    {
//        //直接给前端响应500并输出系统繁忙
//        var_dump($throwable->getMessage());
//        $this->response()->withStatus(Status::CODE_INTERNAL_SERVER_ERROR);
//        $this->response()->write('系统繁忙,请稍后再试 ');
//    }


    /**
     * 验证操作
     * @param string|null $action
     * @return bool|null
     *
     */
    protected function onRequest(?string $action): ?bool
    {

        $ret =  parent::onRequest($action);
        if($ret === false){
            return false;
        }
        $param = $this->request()->getRequestParam();
        //获取自动验证规则
        $Validate = ValidateTool::adminValidateRule($this->request()->getUri()->getPath(),$param);
        if($Validate){
            $ret = $Validate->validate($param);
            if($ret == false){
                $this->returnData(999,['param_name'=> $Validate->getError()->getField()],$Validate->getError()->__toString());
                return false;
            }else{

                $data = $Validate->getVerifiedData();


                Register::getInstance()->set('params',$data);
            }

        }
        return true;
    }


    protected function getUserId(){
        return Register::getInstance()->get('mid');
    }
    protected function getUser(){
        return Register::getInstance()->get('manager_info');
    }
    protected function getRoleId(){
        return Register::getInstance()->get('manager_info')['role_id'];
    }

    /**
     * 生成代码
     * @param $template_path
     * @param $createClassPath
     * @param $class_name
     * @return bool|int
     */
    protected function generateCode($template_path,$createClassPath,$class_name,$task_class_name){

//        $template_path = "./App/Crontab/customize_lottery/TaskTemplate.php";
//        $createClassPath = "./App/Crontab/customize_lottery/";
//        $class_name = "Class";
        if(!file_exists($template_path)){

            return 0;
        }
        if(file_exists($class_name)){

            return 0;
        }
        $code = file_get_contents($template_path);

        $code = str_replace('TaskTemplate',$class_name,$code);
        $code = str_replace('task_class_name',$task_class_name,$code);

        $res = file_put_contents($createClassPath.$class_name.".php",$code);

        if($res){
            return true;
        }else{
            return false;
        }

    }

    protected function setUserCaChe($uid,$user){

        Redis::defer('redis')->hSet(RedisKey::USER_INFO_HASH,'uid:'.$uid,json_encode($user));

    }
}